﻿#region using statements

using System;

using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

using NGen;
using NGen.Managers.Collision;
using NGen.Managers.GameObject;

#endregion

namespace XNAGameDemo.GameObjects
{
	public class CollidingBlock : GameObject2D, ICollidable2D
	{
		private BoundingBox m_boundingBox;

		private Vector2 m_velocity;

		protected override void _Initialize() {
			base._Initialize();
			m_texture = GameEngine.GameContent.Load<Texture2D>("Content\\Textures\\block");

			m_scale.X = 0.1f;
			m_scale.Y = 0.1f;

			m_origin.X = m_texture.Width * m_scale.X / 2.0f;
			m_origin.Y = m_texture.Height * m_scale.Y / 2.0f;

			m_boundingBox = new BoundingBox(new Vector3(-m_origin.X, -m_origin.Y, 0f), new Vector3(m_texture.Width * m_scale.X, m_texture.Height * m_scale.Y, 1.0f));

			m_velocity = new Vector2((float) GameEngine.Random.NextDouble(), (float) GameEngine.Random.NextDouble());
			m_velocity.Normalize();
			m_velocity *= 5;

			m_textureTint = new Color((float) GameEngine.Random.NextDouble(), (float) GameEngine.Random.NextDouble(), (float) GameEngine.Random.NextDouble());

			
			GameEngine.GetService<IManagerCollision2D>().RegisterCollidable(Id, false);
		}

		protected override void _Cleanup() {
			base._Cleanup();
			GameEngine.GetService<IManagerCollision2D>().UnRegisterCollidable(Id);
		}

		protected override void _Update(GameTime p_gameTime) {

			
			m_position += m_velocity;

			if(m_position.X + m_texture.Bounds.Width * m_scale.X - m_origin.X * m_scale.X > GameEngine.GameWindow.ClientBounds.Width ||
			   m_position.X + m_velocity.X - m_origin.X * m_scale.X < 0) {
				m_velocity.X *= -1f;
			}
			if(m_position.Y + m_texture.Bounds.Height * m_scale.Y - m_origin.Y * m_scale.Y > GameEngine.GameWindow.ClientBounds.Height ||
			   m_position.Y + m_velocity.Y - m_origin.Y * m_scale.Y < 0) {
				m_velocity.Y *= -1f;
			}

			m_zDepth = 1 - (m_position.Y / GameEngine.GameWindow.ClientBounds.Height + .001f);
		}

		//protected override void _Draw(GameTime p_gameTime) {
		//	if(m_texture == null) {
		//		return;
		//	}

		//	GameEngine.SpriteBatch.Draw(m_texture, m_position, null, m_textureTint, 0.0f, m_origin, m_scale, SpriteEffects.None, m_zDepth);
		//}

		#region ICollidable implementation

		public BoundingBox BoundingBox {
			get { return m_boundingBox; }
			set { m_boundingBox = value; }
		}

		public void Collided(CollisionMessage2D p_objectCollidedWith) {
			if(p_objectCollidedWith.m_objectType ==
			   typeof(MouseBall)) {
				m_position.X = GameEngine.GameWindow.ClientBounds.Width / 5.0f * (GameEngine.Random.Next(4) + 1);
				m_position.Y = GameEngine.GameWindow.ClientBounds.Height / 3.0f * (GameEngine.Random.Next(2) + 1);
				m_textureTint = new Color((float)GameEngine.Random.NextDouble(), (float)GameEngine.Random.NextDouble(), (float)GameEngine.Random.NextDouble());
			} else if(p_objectCollidedWith.m_objectType ==
			          typeof(CollidingBlock)) {
				m_textureTint = GameEngine.GetService<IManagerGameObjects>().GetGameObjectById<GameObject2D>(p_objectCollidedWith.m_objectId).m_textureTint;
			}
		}

		#endregion
	}
}